package com.hrm.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.hrm.dao.RewardMapper;
import com.hrm.entity.Reward;
import com.hrm.service.RewardService;
import com.hrm.util.Page;

public class RewardServiceImpl implements RewardService {
	private static final Log log = LogFactory.getLog(RewardServiceImpl.class); 
	@Autowired
	private RewardMapper rewardMapper;
	public RewardMapper getRewardMapper() {
		return rewardMapper;
	}

	public void setRewardMapper(RewardMapper rewardMapper) {
		this.rewardMapper = rewardMapper;
	}
	

	public List<Reward> getRewards() {
		log.info("****** Do method getRewards() begin,no params info.");
		List<Reward> rewardTypes=rewardMapper.selectAllReward();
		log.info("****** Do method getRoles() end,return info ==== [ List<Reward>="+rewardTypes+" ]");
		return rewardTypes;
	}
	public Reward getRewardById(long id) {
		log.info("****** Do method getRewardById(long id) begin,params info ==== [ id="+id+" ]");
		Reward rewardType =  rewardMapper.selectRewardById(id);
		log.info("****** Do method getRewardById(long id) end,return info ==== [ Reward="+rewardType+" ]");
		return rewardType;
	}

	public int addReward(Reward rewardType) {
		log.info("****** Do method addReward(Reward rewardType) begin,params info ==== [ rewardType="+rewardType+" ]");
		int cnts=-1;
		cnts= rewardMapper.insertReward(rewardType);
		log.info("****** Do method addReward(Reward rewardType) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public int updateReward(Reward rewardType) {
		log.info("****** Do method updateReward(Reward rewardType) begin,params info ==== [ rewardType="+rewardType+" ]");
		int cnts=-1;
		cnts= rewardMapper.updateReward(rewardType);
		log.info("****** Do method updateReward(Reward rewardType) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public int deleteRewardById(long id) {
		log.info("****** Do method deleteRewardById(long id) begin,params info ==== [ id="+id+" ]");
		int cnts=-1;
		cnts= rewardMapper.deleteRewardById(id);
		log.info("****** Do method deleteRewardById(long id) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}
	public List<Reward> getRewards(Page page) {
		log.info("****** Do method getRewards(Page page) begin,params info ==== [page="+page+"]");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("currentStart", page.getCurrentStart()-1);
		map.put("currentEnd", page.getCurrentStart()+page.getPagesize()-1);
		List<Reward> rewardTypes = rewardMapper.selectRewardByMap(map);
		log.info("****** Do method getRewards(Page page) end,return info ==== [ List<Reward>="+rewardTypes+" ]");
		return rewardTypes;
	}

	public int getRewardCounts() {
		log.info("****** Do method getRewardCounts() begin,no params info.");
		int cnts=-1;
		cnts= rewardMapper.getAllRewardCounts();
		log.info("****** Do method getRewardCounts() end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}
	
	public int deleteRewardById(String ids) {
		log.info("****** Do method deleteRewardById(String ids) begin,params info ==== [ ids="+ids+" ]");
		Map<String,Object> map = new HashMap<String,Object>();
		String[] tmp=ids.split(",");
		Integer[] tids = new Integer[tmp.length];
		for(int i=0;i<tmp.length;i++){
			tids[i]=Integer.parseInt(tmp[i]);
		}
		List<Integer> configItems=Arrays.asList(tids);
		map.put("ids", configItems);
		int cnts=-1;
		cnts=rewardMapper.deleteRewardByMap(map);
		log.info("****** Do method deleteRewardById(String ids) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

}
